/**
 * @BelongsProject : LeetCode
 * @Author : WXY
 * @CreateTime : 2022-12-07  23:22
 * @Description : 剑 指 Offer 18. 删除链表的节点
 * @Version : 1.0
 */

public class Num018_deleteNode {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static ListNode deleteNode(ListNode head, int val) {
      ListNode next = null;
      ListNode pre = null;
      ListNode head_head = head;
        while (head != null) {
            if (head.val == val) {
               break;
            }
            pre = head;
            head = head.next;
        }
        if (head.next == null) {
            pre.next = null;
        } else {
            next = head.next;
            head.val = next.val;
            head.next = next.next;
        }
        return head_head;
    }
}
